package cn.k.service.impl;

import cn.k.domain.SystemDictionary;
import cn.k.mapper.SystemDictionaryMapper;
import cn.k.qo.QueryObject;
import cn.k.qo.SystemDictionaryQueryObject;
import cn.k.service.ISystemDictionaryService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SystemDictionaryServiceImpl implements ISystemDictionaryService {
    @Autowired
    private SystemDictionaryMapper systemDictionaryMapper;

    @Override
    @Transactional
    public void insert(SystemDictionary systemDictionary) {
        systemDictionaryMapper.insert(systemDictionary);
    }

    @Override
    public void delete(Long id) {
        systemDictionaryMapper.deleteByPrimaryKey(id);

    }

    @Override
    @Transactional
    public void update(SystemDictionary systemDictionary) {
        systemDictionaryMapper.updateByPrimaryKey(systemDictionary);

    }

    @Override
    public SystemDictionary get(Long id) {
        SystemDictionary systemDictionary = systemDictionaryMapper.selectByPrimaryKey(id);
        return systemDictionary;
    }

    @Override
    public List<SystemDictionary> selectAll() {
        List<SystemDictionary> systemDictionarys = systemDictionaryMapper.selectAll();
        return systemDictionarys;
    }


    @Override
    public List<SystemDictionary> selectForList(SystemDictionaryQueryObject qo) {
        List<SystemDictionary> systemDictionarys = systemDictionaryMapper.selectForList(qo);
        return systemDictionarys;
    }

    @Override
    public PageInfo<SystemDictionary> query(SystemDictionaryQueryObject qo) {
        //告诉插件我们查询第几页,每页多少条
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        //查询当前页数据
        List<SystemDictionary> systemDictionarys = systemDictionaryMapper.selectForList(qo);
        return new PageInfo<>(systemDictionarys);
    }


    @Override
    public List<Map> queryTreeData() {

        //查询顶级数据目录(父节点)-parent_id 为null
        List<SystemDictionary> all = systemDictionaryMapper.selectAll();
        ArrayList<SystemDictionary> top = new ArrayList<>();

        //遍历所有数据
        for (SystemDictionary item : all) {
            //拿到根节点
            if (item.getParentId() == null) {
                top.add(item);
                //item.setChildren(findChildren(all, item.getId()));
            }
        }
        //查询二级(下级)数据路径,查询儿子(子节点)
        fillChildren(all, top);

        //使用map进行数据改造
        String str = JSON.toJSONString(top);
        str=str.replaceAll("name","text").replaceAll("children","nodes");

        //参数1:list集合json格式字符串,参数2:集合泛型
        List<Map> maps = JSON.parseArray(str, Map.class);

        return maps;
    }

    @Override
    public List<SystemDictionary> queryByService(String sn) {
        return systemDictionaryMapper.queryByService(sn);
    }

    @Override
    public List<SystemDictionary> queryByParentId(Long parentId) {
        return systemDictionaryMapper.selectByParentId(parentId);
    }

    //填充儿子
    private void fillChildren(List<SystemDictionary> all, List<SystemDictionary> children) {
        //如果儿子为空那就不找了
        if (children == null || children.size() == 0) {
            return;
        }
        //儿子还有,找儿子
        for (SystemDictionary item : children) {
            item.setChildren(findChildren(all, item.getId()));


            //儿子找儿子
            fillChildren(all, item.getChildren());
        }


    }

    /**
     * 参数1 :所有的数据 参数2:父id
     *
     * @param all
     * @param parentId
     * @return
     */
    private List<SystemDictionary> findChildren(List<SystemDictionary> all, Long parentId) {
        ArrayList<SystemDictionary> children = new ArrayList<>();
        //遍历所有数据
        for (SystemDictionary item : all) {
            //判断,如果遍历的数据的父id等于我传入的id(父类本身的id)
            if (item.getParentId() == parentId) {
                //那么儿子就把父id加进去
                children.add(item);
            }
        }
        return children;

    }


}
